package com.app.base.tool;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.app.application.QXApplication;
import com.app.utils.CommonUtil;
import com.app.utils.JSONTool;
import com.app.utils.SharePrefUtil;
import com.app.utils.TransformController;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.socialize.UMShareAPI;

import org.xutils.x;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

/**
 * 时间:2015-7-14
 * 公司:DDY
 * 作者:LBH
 * 功能:Activity基类  简化相同功能
 * @author Administrator
 *
 */
public abstract class BaseActivity extends AppCompatActivity {
	public String TAG = "QX_TAG";
	public boolean checkNetType;
	public DisplayImageOptions options;
	public static HashMap<String,String> mapKeys = new HashMap<String,String>();
	//全部activity 都使用这个上下文
	public Context mContext;
	public boolean mIsRunning;


	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题
//		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//				WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏

		mContext = QXApplication.getContext();

		checkNetType = CommonUtil.isNetWork();

		x.view().inject(this);

		initSucessView();
		
		initData(savedInstanceState);
		
		initViewHandler();
	}
	

	/**
	 * 设置显示页面
	 * @return
	 */
	public abstract void initSucessView();
	

	/**
	 * 数据处理
	 * @param savedInstanceState
	 */
	public abstract void initData(Bundle savedInstanceState);

	/**
	 * 处理特殊点击/相应事件
	 */
	public abstract void initViewHandler();
	

	/**
	 * 点击显示 时间间隔在1000
	 * @param v
	 * @return
     */
	public boolean onClickTime(View v){
		boolean isClick = false;
		final int MIN_CLICK_DELAY_TIME = 1000;
		Object lastClickTime;

		long currentTime = Calendar.getInstance().getTimeInMillis();

		lastClickTime = v.getTag();

		if(((lastClickTime == null)||((Long)lastClickTime == 0) )||(currentTime - (Long)lastClickTime) > MIN_CLICK_DELAY_TIME){
			v.setTag(currentTime);
			isClick = true;
		}

		return isClick;
	}

	/**
	 * 处理全局星星
	 * @param number
	 * @return
     */
	public float star(float number){
		return (number <= 0)? 0 :(number/2);
	}

	/**
	 * 传递model 默认传的Boolean
	 * @return
     */
	public Object  getTransModels(){
		Object obj = Boolean.class;
		Intent intent = getIntent();
		if(intent != null){
			Bundle bundle = intent.getBundleExtra(TransformController.ChangeKEY.INTENT_KEY);
			if(bundle != null){
				obj = bundle.getParcelable(TransformController.ChangeKEY.BUNDLE_KEY);
			}
		}
		return obj;
	}

	/**
	 * 判断弹出窗体 存在关闭
	 * @param popu
     */
	public void popupWindowDiscuss(PopupWindow popu){
		if(popu != null && popu.isShowing()){
			popu.dismiss();;
		}
	}
	
	/**
	 * 设置界面文字展示 统一使用html封装
	 * @param textView
	 * @param content
	 */
	public void textViewHTTPText(TextView textView, String content){
		textViewSetText(textView,content);
	}
	
	/**
	 * 判断(字符窜)返回值是否存在
	 * @param requestStr
	 * @return
	 */
	public boolean isRequestStr(String requestStr){
		return (requestStr!=null && requestStr.length()>0 && !requestStr.equals("[]"))?true:false;
	}
	
	/**
	 * 获取的属性 为空转化为""
	 * @param attribute
	 * @return
	 */
	public String setAttributeText(Object attribute){
		String str = (attribute == null) ?"":attribute.toString();
		return str;
	}

	/**
	 * 设置界面文字展示 统一使用html封装
	 * @param textView
	 * @param content
	 */
	public void textViewSetText(TextView textView, String content){
		if(isRequestStr(content)){
			Spanned fromHtml = Html.fromHtml(content);
			textView.setText(fromHtml);
		}else{
			textView.setText(setAttributeText(content));
		}
	}

	/**
	 * 判断(List)返回值是否存在
	 * @param <T>
	 * @param requestList
	 * @return
	 */
	public <T> boolean isRequestList(List<T> requestList){
		return (requestList!=null && !requestList.isEmpty() )?true:false;
	}

	/**
	 * 关闭弹出窗体
	 * @param dialog
     */
	public void dismissDialog(AlertDialog dialog){
		if(dialog != null && dialog.isShowing()){
			dialog.dismiss();
		}
	}

	/**
	 * 获取Edit内容
	 * @param v
	 * @return
     */
	public String getEditToString(TextView v){
		String str = "";
		if(v!= null){
			str = v.getText().toString().trim();
		}
		return str;
	}

	/**
	 * 判断显示 提交之类按钮
	 * @param v
	 * @param resId
     * @return
     */
	public boolean isEmptyOrHint(TextView v,int resId){
		String content = getEditToString(v);
		return (isRequestStr(content) && !content.equals(getString(resId)));
	}

	/**
	 * 判断显示 提交之类按钮
	 * @param v
	 * @param resId
     * @return
     */
	public boolean isEmptyOrHintMinCount(TextView v,int resId,int minCount){
		String content = getEditToString(v);
		boolean isSucess = (isRequestStr(content) && !content.equals(getString(resId)));
		if(isSucess){
			isSucess = (content.length()>=minCount);
		}
		return isSucess;
	}


	/**
	 * 字符窜和资源文件拼接
	 * @param resId
	 * @param content
	 * @return
	 */
	public String addStr(int resId,String content){
		StringBuffer buffer = new StringBuffer("");
		if(resId>0)
			buffer.append(QXApplication.getContext().getResources().getString(resId));
		buffer.append(setAttributeText(content));
		return buffer.toString();
	}

	/**
	 * 获取当前数据状态
	 * @param result
	 * @return
     */
	public boolean requestStatus(String result){
		boolean isSucess;
		int status = JSONTool.requestJSONfindNameCount(result,null,JSONTool.Enum.STATUS);
		return (status == 0) ? true :false;
	}


	private String getName(){
		return getClass().getSimpleName();
	}


	/**
	 * 设置图片
	 * @param path
	 * @param img
	 * @param tempOption //图片不传递Option 默认初始化的设置
     */
	public void setImgPath(String path,ImageView img,DisplayImageOptions tempOption){
		if(tempOption == null){
			tempOption = options;
		}
//		if(SystemPrintl.isDebug)
//			path = ApplicationConfig.DEBUG_IMG;

		ImageLoader.getInstance().displayImage(path,img,tempOption);
	}


	/**
	 * 判断登录状态
	 * @return
     */
	public boolean isUserLogin(){
		return isRequestStr(SharePrefUtil.getString(SharePrefUtil.KEY.APP_USER,""));
	}

	/**
	 * U_MENG
	 */
//	public void onResume() {
//		super.onResume();
//		MobclickAgent.onPageStart(getName());
//		MobclickAgent.onResume(this);
//	}
	
	public void onPause() {
		super.onPause();
//		MobclickAgent.onPageEnd(getName());
//		MobclickAgent.onPause(this);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get(this).onActivityResult(requestCode,resultCode,data);
	}

	/**
	 * 关闭软键盘
	 */
	public void closeKeyboard() {
		View view = getWindow().peekDecorView();
		if (view != null) {
			InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
		}
	}


}
